/*     */ package com.ims.iaa.JavaScript;
/*     */ 
/*     */ import com.ims.iaa.IaaUtil;
/*     */ import com.ims.iaa.URLUtil;
/*     */ import com.ims.iaa.html.ImpAnalyzer;
/*     */ import com.ims.iaa.html.ImpAnalyzerImpl;
/*     */ import com.ims.iaa.html.TagAttributeParser;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PrintStream;
/*     */ import java.io.Reader;
/*     */ import java.net.MalformedURLException;
/*     */ import java.net.URL;
/*     */ import java.net.URLConnection;
/*     */ import java.util.HashMap;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import org.mozilla.javascript.Context;
/*     */ import org.mozilla.javascript.Function;
/*     */ import org.mozilla.javascript.JavaScriptException;
/*     */ import org.mozilla.javascript.NativeArray;
/*     */ import org.mozilla.javascript.Scriptable;
/*     */ import org.mozilla.javascript.ScriptableObject;
/*     */ 
/*     */ public class JavaScriptParser
/*     */ {
/*     */   public static final String MAP_POPUP_URL = "URL";
/*     */   public static final String MAP_POPUP_CONTENT = "CNT";
/*     */   protected static final String SCRIPT_START = "<script";
/*     */   protected static final String SCRIPT_END = "</script>";
/*  49 */   protected static final int SCRIPT_END_LEN = "</script>".length();
/*     */   protected static final String SRC = " src=";
/*  51 */   protected static final int SRC_LEN = " src=".length();
/*     */ 
/*  53 */   private boolean m_bIsIniSuccessful = false;
/*     */   protected Context m_cx;
/*     */   protected Scriptable m_scope;
/*     */   private Scriptable m_window;
/*     */   private Scriptable m_document;
/*     */   private Function m_fnWin_Popupcount;
/*     */   private Function m_fnWin_ClearPopup;
/*     */   private Function m_fnWind_ClearPopupDocument;
/*     */   private Function m_fnWin_GetPopupURL;
/*     */   private Function m_fnWin_GetPopupContent;
/*     */   private Function m_fnDoc_GetContent;
/*     */   private Function m_fnDoc_ClearContent;
/*     */   private URL m_baseURL;
/*  79 */   private String m_sUserAgent = null;
/*     */ 
/*  81 */   private PrintStream m_scriptOut = null;
/*     */ 
/*  83 */   private ImpAnalyzer m_impAnalyzer = null;
/*     */ 
/*     */   public JavaScriptParser(URL baseUrl, ImpAnalyzer impa) {
/*  86 */     this(baseUrl, impa, null);
/*     */   }
/*     */ 
/*     */   public JavaScriptParser(URL baseUrl, ImpAnalyzer impa, PrintStream out) {
/*  90 */     this.m_baseURL = baseUrl;
/*  91 */     this.m_cx = Context.enter();
/*     */ 
/*  94 */     this.m_scope = this.m_cx.initStandardObjects();
/*     */ 
/*  96 */     this.m_bIsIniSuccessful = initialHTMLObject();
/*     */ 
/*  98 */     this.m_scriptOut = out;
/*     */ 
/* 100 */     this.m_impAnalyzer = impa;
/* 101 */     this.m_sUserAgent = this.m_impAnalyzer.getUserAgent();
/*     */   }
/*     */ 
/*     */   public boolean IsIniSuccessful()
/*     */   {
/* 107 */     return this.m_bIsIniSuccessful;
/*     */   }
/*     */ 
/*     */   public boolean changeLocation(URL url)
/*     */   {
/*     */     try
/*     */     {
/* 117 */       this.m_baseURL = url;
/*     */ 
/* 119 */       String sScript = HTMLObject.getRefreshLocationStr(this.m_baseURL);
/*     */ 
/* 121 */       this.m_cx.evaluateString(this.m_scope, sScript, "MySource", 1, null);
/*     */ 
/* 123 */       return true;
/*     */     } catch (JavaScriptException jse) {
/* 125 */       IaaUtil.printStackTrace(jse);
/* 126 */       return false;
/*     */     } catch (Exception e) {
/* 128 */       IaaUtil.printStackTrace(e);
/* 129 */     }return false;
/*     */   }
/*     */ 
/*     */   private boolean initialHTMLObject()
/*     */   {
/*     */     try
/*     */     {
/* 138 */       HTMLObject html = HTMLObject.getInstance();
/* 139 */       if (html == null)
/*     */       {
/* 141 */         return false;
/*     */       }
/* 143 */       String sScript = html.getObjectScript(this.m_baseURL);
/*     */ 
/* 145 */       this.m_cx.evaluateString(this.m_scope, sScript, "MySource", 1, null);
/*     */ 
/* 147 */       return getScriptObject();
/*     */     } catch (JavaScriptException jse) {
/* 149 */       IaaUtil.printStackTrace(jse);
/*     */ 
/* 151 */       return false;
/*     */     } catch (Exception e) {
/* 153 */       IaaUtil.printStackTrace(e);
/*     */     }
/* 155 */     return false;
/*     */   }
/*     */ 
/*     */   private boolean getScriptObject()
/*     */   {
/* 160 */     boolean bState = true;
/* 161 */     String sObj = "";
/* 162 */     this.m_window = ((Scriptable)this.m_scope.get("window", this.m_scope));
/* 163 */     if (this.m_window == null) {
/* 164 */       sObj = "window object";
/* 165 */       bState = false;
/*     */     }
/* 167 */     if (bState) {
/* 168 */       this.m_document = ((Scriptable)this.m_scope.get("document", this.m_scope));
/* 169 */       if (this.m_document == null) {
/* 170 */         sObj = "document object";
/* 171 */         bState = false;
/*     */       }
/*     */     }
/* 174 */     if (bState) {
/* 175 */       this.m_fnWin_Popupcount = ((Function)ScriptableObject.getProperty(this.m_window, "getPopupCount"));
/* 176 */       if (this.m_fnWin_Popupcount == null) {
/* 177 */         sObj = "getPopupCount function";
/* 178 */         bState = false;
/*     */       }
/*     */     }
/* 181 */     if (bState) {
/* 182 */       this.m_fnWin_ClearPopup = ((Function)ScriptableObject.getProperty(this.m_window, "clearPopup"));
/* 183 */       if (this.m_fnWin_Popupcount == null) {
/* 184 */         sObj = "clearPopup function";
/* 185 */         bState = false;
/*     */       }
/*     */     }
/* 188 */     if (bState) {
/* 189 */       this.m_fnWind_ClearPopupDocument = ((Function)ScriptableObject.getProperty(this.m_window, "clearPopupDocument"));
/* 190 */       if (this.m_fnWind_ClearPopupDocument == null) {
/* 191 */         sObj = "clearPopupDocument function";
/* 192 */         bState = false;
/*     */       }
/*     */     }
/* 195 */     if (bState) {
/* 196 */       this.m_fnWin_GetPopupURL = ((Function)ScriptableObject.getProperty(this.m_window, "getPopupURL"));
/* 197 */       if (this.m_fnWin_Popupcount == null) {
/* 198 */         sObj = "getPopupURL function";
/* 199 */         bState = false;
/*     */       }
/*     */     }
/* 202 */     if (bState) {
/* 203 */       this.m_fnWin_GetPopupContent = ((Function)ScriptableObject.getProperty(this.m_window, "getPopupContent"));
/* 204 */       if (this.m_fnWin_Popupcount == null) {
/* 205 */         sObj = "getPopupContent function";
/* 206 */         bState = false;
/*     */       }
/*     */     }
/* 209 */     if (bState) {
/* 210 */       this.m_fnDoc_GetContent = ((Function)ScriptableObject.getProperty(this.m_document, "getContent"));
/* 211 */       if (this.m_fnWin_Popupcount == null) {
/* 212 */         sObj = "getContent function";
/* 213 */         bState = false;
/*     */       }
/*     */     }
/* 216 */     if (bState) {
/* 217 */       this.m_fnDoc_ClearContent = ((Function)ScriptableObject.getProperty(this.m_document, "clearContent"));
/* 218 */       if (this.m_fnWin_Popupcount == null) {
/* 219 */         sObj = "clearContent function";
/* 220 */         bState = false;
/*     */       }
/*     */     }
/* 223 */     if (!bState) {
/* 224 */       IaaUtil.log("Fatal Error, can't get " + sObj + " from javascript.");
/*     */     }
/* 226 */     return bState;
/*     */   }
/*     */ 
/*     */   public boolean ParseScriptTag(String sScript)
/*     */   {
/* 236 */     sScript = getScriptContent(sScript);
/*     */ 
/* 238 */     return ParseScriptContent(sScript);
/*     */   }
/*     */ 
/*     */   public boolean ParseScriptContent(String sScript)
/*     */   {
/*     */     try
/*     */     {
/* 249 */       if (this.m_scriptOut != null) {
/*     */         try {
/* 251 */           this.m_scriptOut.println(sScript);
/* 252 */           this.m_scriptOut.println("//////////////////////new script \r\n");
/*     */         }
/*     */         catch (Exception localException1)
/*     */         {
/*     */         }
/*     */       }
/* 258 */       this.m_cx.evaluateString(this.m_scope, sScript, "MySource", 1, null);
/* 259 */       return true;
/*     */     }
/*     */     catch (JavaScriptException jse) {
/* 262 */       JSUtil.SaveJS(sScript, jse.toString(), this.m_baseURL.toExternalForm());
/* 263 */       IaaUtil.log("Parse Script Error, Script detail see ErrorScript.txt");
/*     */ 
/* 265 */       return false;
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 270 */       JSUtil.SaveJS(sScript, "Parse JavaScript error: " + e.toString(), this.m_baseURL.toExternalForm());
/* 271 */       return false;
/*     */     }
/*     */     catch (Throwable t)
/*     */     {
/* 275 */       JSUtil.SaveJS(sScript, "Parse JavaScript error: " + t.toString(), this.m_baseURL.toExternalForm());
/* 276 */     }return false;
/*     */   }
/*     */ 
/*     */   protected String getScriptContent(String sScript)
/*     */   {
/* 292 */     int iIndex = sScript.indexOf(">");
/* 293 */     if (iIndex < 0) {
/* 294 */       return "";
/*     */     }
/* 296 */     String sStartTag = sScript.substring(0, iIndex + 1);
/* 297 */     String sJudge = sStartTag.toLowerCase();
/*     */ 
/* 299 */     if (sJudge.indexOf("<script") < 0) {
/* 300 */       return "";
/*     */     }
/* 302 */     String sSrc = null;
/*     */     try {
/* 304 */       TagAttributeParser tag = new TagAttributeParser(sStartTag);
/*     */ 
/* 306 */       sSrc = tag.getValue("src");
/*     */ 
/* 308 */       String sLan = tag.getValue("language");
/* 309 */       if (sLan != null) {
/* 310 */         sLan = sLan.toLowerCase();
/*     */ 
/* 312 */         if (sLan.indexOf("javascript") < 0)
/* 313 */           return "";
/*     */       }
/*     */     }
/*     */     catch (Exception e) {
/* 317 */       IaaUtil.printStackTrace(e);
/* 318 */       IaaUtil.log("tag Like this:\n" + sStartTag);
/* 319 */       return "";
/*     */     }
/* 321 */     if (sSrc != null)
/*     */     {
/*     */       try
/*     */       {
/* 328 */         URL scriptURl = URLUtil.getURL(this.m_baseURL, sSrc);
/*     */ 
/* 330 */         if (this.m_impAnalyzer.isDisallow(scriptURl)) {
/* 331 */           IaaUtil.log("Obey robots.txt: " + scriptURl);
/* 332 */           return "";
/*     */         }
/* 334 */         URLConnection uc = scriptURl.openConnection();
/*     */ 
/* 336 */         uc.setRequestProperty("User-Agent", this.m_sUserAgent);
/*     */ 
/* 338 */         uc.setRequestProperty("Connection", "Keep-Alive");
/*     */ 
/* 340 */         int bufSize = 4096;
/* 341 */         Reader preReader = new InputStreamReader(uc.getInputStream());
/* 342 */         char[] buf = new char[bufSize];
/* 343 */         StringBuffer sbHtml = new StringBuffer(bufSize);
/*     */         while (true) {
/* 345 */           int moved = preReader.read(buf);
/* 346 */           if (moved < 0) break;
/* 347 */           sbHtml.append(buf, 0, moved);
/*     */         }
/* 349 */         preReader.close();
/*     */ 
/* 351 */         return sbHtml.substring(0);
/*     */       } catch (IOException e) {
/* 353 */         IaaUtil.log("IOException while get Script content form " + sSrc);
/* 354 */         return "";
/*     */       } catch (Exception e) {
/* 356 */         IaaUtil.printStackTrace(e);
/* 357 */         return "";
/*     */       }
/*     */     }
/* 360 */     int iEnd = sScript.lastIndexOf("</");
/* 361 */     return sScript.substring(sStartTag.length(), iEnd);
/*     */   }
/*     */ 
/*     */   public String getDocument()
/*     */   {
/* 370 */     if (this.m_fnDoc_GetContent != null) {
/*     */       try {
/* 372 */         return Context.toString(this.m_fnDoc_GetContent.call(this.m_cx, this.m_scope, this.m_document, new Object[0]));
/*     */       } catch (Exception e) {
/* 374 */         IaaUtil.printStackTrace(e);
/*     */       }
/*     */     }
/* 377 */     return "";
/*     */   }
/*     */ 
/*     */   public boolean clearDocument()
/*     */   {
/* 384 */     if (this.m_fnDoc_ClearContent != null) {
/*     */       try {
/* 386 */         this.m_fnDoc_ClearContent.call(this.m_cx, this.m_scope, this.m_document, new Object[0]);
/* 387 */         return true;
/*     */       } catch (Exception e) {
/* 389 */         IaaUtil.printStackTrace(e);
/*     */       }
/*     */     }
/* 392 */     return false;
/*     */   }
/*     */ 
/*     */   public HashMap getPopup()
/*     */   {
/* 412 */     HashMap map = new HashMap();
/* 413 */     LinkedList lstURL = new LinkedList();
/* 414 */     LinkedList lstContent = new LinkedList();
/* 415 */     map.put("URL", lstURL);
/* 416 */     map.put("CNT", lstContent);
/* 417 */     if ((this.m_window != null) && 
/* 418 */       (this.m_fnWin_Popupcount != null) && 
/* 419 */       (this.m_fnWin_GetPopupURL != null) && 
/* 420 */       (this.m_fnWin_GetPopupContent != null))
/*     */     {
/* 423 */       int iCount = Integer.parseInt(Context.toString(this.m_fnWin_Popupcount.call(this.m_cx, this.m_scope, this.m_window, new Object[0])));
/*     */ 
/* 425 */       if (iCount > 0) {
/*     */         try
/*     */         {
/* 428 */           Object objRet = this.m_fnWin_GetPopupURL.call(this.m_cx, this.m_scope, this.m_window, new Object[0]);
/* 429 */           if (objRet instanceof NativeArray)
/*     */           {
/* 431 */             NativeArray arRet = (NativeArray)objRet;
/* 432 */             for (int i = 0; i < arRet.getLength(); ++i)
/*     */             {
/* 434 */               lstURL.add(arRet.get(i, this.m_window).toString());
/*     */             }
/*     */           } else {
/* 437 */             IaaUtil.log("Fatal Error, can't get popup URL from javascript.");
/*     */           }
/*     */ 
/* 441 */           objRet = this.m_fnWin_GetPopupContent.call(this.m_cx, this.m_scope, this.m_window, new Object[0]);
/* 442 */           if (objRet instanceof NativeArray)
/*     */           {
/* 444 */             NativeArray arRet = (NativeArray)objRet;
/* 445 */             for (int i = 0; i < arRet.getLength(); ++i)
/*     */             {
/* 447 */               lstContent.add(arRet.get(i, this.m_window).toString());
/*     */             }
/*     */           } else {
/* 450 */             IaaUtil.log("Fatal Error, can't get popup content from javascript.");
/*     */           }
/*     */         } catch (Exception e) {
/* 453 */           IaaUtil.printStackTrace(e);
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 458 */     return map;
/*     */   }
/*     */ 
/*     */   public boolean clearPopupContent(int iIndex)
/*     */   {
/* 468 */     if (this.m_fnWind_ClearPopupDocument != null)
/*     */     {
/* 470 */       Object[] param = { iIndex };
/*     */       try {
/* 472 */         this.m_fnWind_ClearPopupDocument.call(this.m_cx, this.m_scope, this.m_window, param);
/* 473 */         return true;
/*     */       } catch (Exception e) {
/* 475 */         IaaUtil.printStackTrace(e);
/*     */       }
/*     */     }
/* 478 */     return false;
/*     */   }
/*     */ 
/*     */   public String getPopupURL(int iIndex)
/*     */   {
/* 487 */     if (this.m_fnWin_GetPopupURL != null)
/*     */     {
/* 489 */       Object[] param = { iIndex };
/*     */       try {
/* 491 */         return (String)this.m_fnWin_GetPopupURL.call(this.m_cx, this.m_scope, this.m_window, param);
/*     */       } catch (Exception e) {
/* 493 */         IaaUtil.printStackTrace(e);
/*     */       }
/*     */     }
/* 496 */     return "";
/*     */   }
/*     */ 
/*     */   public String getPopupContent(int iIndex)
/*     */   {
/* 505 */     if (this.m_fnWin_GetPopupContent != null)
/*     */     {
/* 507 */       Object[] param = { iIndex };
/*     */       try {
/* 509 */         return (String)this.m_fnWin_GetPopupContent.call(this.m_cx, this.m_scope, this.m_window, param);
/*     */       } catch (Exception e) {
/* 511 */         IaaUtil.printStackTrace(e);
/*     */       }
/*     */     }
/* 514 */     return "";
/*     */   }
/*     */ 
/*     */   public boolean clearPopup()
/*     */   {
/* 520 */     if (this.m_fnWin_ClearPopup != null) {
/*     */       try {
/* 522 */         this.m_fnWin_ClearPopup.call(this.m_cx, this.m_scope, this.m_window, new Object[0]);
/* 523 */         return true;
/*     */       } catch (Exception e) {
/* 525 */         IaaUtil.printStackTrace(e);
/*     */       }
/*     */     }
/* 528 */     return false;
/*     */   }
/*     */   public static void main(String[] args) {
/*     */     try {
/* 532 */       URL url = URLUtil.getURL("http://localhost/");
/* 533 */       JavaScriptParser parser = new JavaScriptParser(url, new ImpAnalyzerImpl());
/* 534 */       if (parser.IsIniSuccessful()) {
/* 535 */         String s = "<script language=javascript>var G_=new Object();G_.loc=self.location;document.write(\"doc1\");\nvar newwin = window.open(\"url1\");\nnewwin.document.write(\"popup 11\", \"popup 12\");\n</script>\n";
/*     */ 
/* 541 */         if (parser.ParseScriptTag(s)) {
/* 542 */           System.out.println(parser.getDocument());
/* 543 */           HashMap map = parser.getPopup();
/* 544 */           if (map != null) {
/* 545 */             List lst = (List)map.get("URL");
/* 546 */             for (int i = 0; i < lst.size(); ++i) {
/* 547 */               System.out.println(lst.get(i).toString());
/*     */             }
/* 549 */             lst = (List)map.get("CNT");
/* 550 */             for (int i = 0; i < lst.size(); ++i)
/* 551 */               System.out.println(lst.get(i).toString());
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     catch (MalformedURLException e) {
/* 557 */       e.printStackTrace();
/* 558 */       return;
/*     */     }
/*     */   }
/*     */ }

/* Location:           X:\Documents and Settings\Administrator\workspace\iaa_changsheng\build\lib\iaa5.0\
 * Qualified Name:     com.ims.iaa.JavaScript.JavaScriptParser
 * JD-Core Version:    0.5.4
 */